from rest_framework import serializers
from oauth.models import Roles


class RolesSerializer(serializers.ModelSerializer):
    """角色管理序列化器"""
    gmt_create  = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', read_only=True)
    class Meta:
        model = Roles
        fields = ['id', 'name', 'permissions', 'desc', 'gmt_create']
        extra_kwargs = {
            'permissions': {
                'read_only': True,
            },
        }


class RolesPartialSerializer(serializers.ModelSerializer):
    """用户局部更新序列化器(角色授权)"""
    class Meta:
        model = Roles
        fields = ['id', 'permissions']

    def validate(self, attrs):
        permissions = attrs.get('permissions')
        for permission in permissions:
            if permission.pid and permission.pid not in permissions:
                raise serializers.ValidationError('缺失父节点权限')
        return attrs
